<template>
  <div class="app-container">
    <div class="header-bar" style="justify-content: flex-end;">
      <el-input
        placeholder="请输入需求编号/名称/关联合同"
        style="width: 240px; float: right; margin-bottom: 10px;"
        v-model="queryParams.keyWord"
        @keyup.enter="handleSearch"
        clearable
      >
        <template #append>
          <el-button icon="Search" @click="handleSearch"></el-button>
        </template>
      </el-input>
    </div>

    <el-table :data="orderList" style="width: 100%" border>
      <el-table-column prop="dno" label="需求编号" align="left" :show-overflow-tooltip="true" />
      <el-table-column prop="dname" label="需求名称" align="left" :show-overflow-tooltip="true" />
      <el-table-column prop="coName" label="关联合同" align="left" :show-overflow-tooltip="true" />
      <el-table-column prop="materialName" label="物料" align="left" :show-overflow-tooltip="true" />
      <el-table-column prop="productionNumber" label="生产加工单数" align="left" />
      <el-table-column prop="successNumber" label="完成数量" align="left" />
      <el-table-column prop="numberSum" label="排产总数" align="left" />
      <el-table-column prop="saleName" label="销售人员" align="left" :show-overflow-tooltip="true" />
      <el-table-column prop="createTime" label="创建时间" align="left" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="left" width="100" fixed="right">
        <template #default="scope">
          <el-link type="primary" @click="handleView(scope.row)">查看详情</el-link>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      style="margin-top: 20px; justify-content: flex-end;"
      v-model:current-page="queryParams.pageNum"
      v-model:page-size="queryParams.pageSize"
      :page-sizes="[10, 20, 30, 50]"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup name="order">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import {
  getProductionOrder
} from '@/api/production'
const router = useRouter();

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  keyWord: ''
})

// 数据总数
const total = ref(3) // 模拟数据总数

// 加载状态
const loading = ref(false)
// 表格数据
const orderList = ref([])

// 获取订单列表
const getOrderList = async () => {
  loading.value = true
  getProductionOrder(queryParams).then(res => {
    loading.value = false
    orderList.value = res.data?.list || [];
    // console.log('res.data?.list', res.data?.list)
    total.value = res.data?.total || 0;
  }).catch(() => {
    orderList.value = [];
    total.value = 0;
  });
}

// 初始化
onMounted(() => {
  getOrderList()
})

// 搜索
function handleSearch() {
  queryParams.pageNum = 1
  getOrderList()
}

// 查看详情
function handleView(row) {
  // 实际项目中这里会跳转到详情页
  // router.push({
  //   path: '/production/scheduleDetail/index'
  // })
  router.push({
    path: `/production/scheduleDetail/index/${row.deId}`
  })
}

// 分页大小改变
function handleSizeChange(val) {
  queryParams.pageSize = val
  queryParams.pageNum = 1
  getOrderList()
}

// 当前页码改变
function handleCurrentChange(val) {
  queryParams.pageNum = val
  getOrderList()
}
</script>

<style scoped>
.app-container {
  padding: 20px;
  box-sizing: border-box;
}

.header-bar {
  margin-bottom: 10px;
  overflow: hidden;
}

.ml5 {
  margin-left: 15px;
}

.pagination-container {
  margin-top: 20px;
  text-align: right;
  box-sizing: border-box;
  width: 100%;
}

:deep(.el-pagination) {
  display: flex;
  justify-content: flex-end;
  flex-wrap: wrap;
}
</style>
